package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wit.witmg.entity.TStock;
import com.wit.witmg.entity.TTool;
import com.wit.witmg.mapper.TToolMapper;
import com.wit.witmg.service.ITToolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2024-12-12
 */
@Service
public class TToolServiceImpl extends ServiceImpl<TToolMapper, TTool> implements ITToolService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        TTool t= JSON.parseObject(json,TTool.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            t.setDate(new Date());
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, String oid, String bn, Integer id, String tp) {
        QueryWrapper<TTool> qw=new QueryWrapper<>();
        if(id!=null){
            return JsonResult.success(getById(id));
        }else if(bn!=null){
            qw.like("bn",bn);
            return JsonResult.success(list(qw));
        }else if(oid!=null){
            qw.likeRight("oid",oid);
            return JsonResult.success(list(qw));
        }else if(tp!=null){
            String[] tps=tp.split(",");
            for (String s : tps) {
                qw.and(wrapper -> wrapper.like("tp", s));
            }
            return JsonResult.success(list(qw));
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }

    @Override
    public Object del(HttpServletRequest request, Integer id) {
        boolean bl= removeById(id);
        if(bl) {
//            remove(new QueryWrapper<TStock>().eq("pid",id));
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_200);
    }
}
